package com.ramesh.fragmenttabhostdemo;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.Spinner;

public class HomeFragment extends Fragment {


	public HomeFragment() {
		// TODO Auto-generated constructor stub

	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View v = LayoutInflater.from(getActivity()).inflate(R.layout.home_page,
				null);
		//text = (TextView) v.findViewById(R.id.text);
		if (getArguments() != null) {
			//
			try {
				String value = getArguments().getString("key");
				///text.setText("Current Tab is: " + value);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return v;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		initCountriesButton();
		initLanguagesSpiner(getView());
		initWebView();
	}
	
	private void initWebView() {
		// TODO Auto-generated method stub
		WebView m_webview = (WebView) getView().findViewById(R.id.webView);
		//Example of thml page
		String html = " <html>" +
                         "<body>" +
                            "<h1>This is heading 1</h1>" +
                         "<a href=\"http://www.google.com\" target=\"_blank\">Visit google.com!</a>" +
                            "<p>Example of test !! Example of test !!Example of test !!Example of test !!Example of test !!Example of test !!Example of test !!Example of test !!Example of test !!Example of test !!Example of test !!Example of test !!Example of test !!Example of test !!Example of test !!Example of test !!Example of test !!Example of test !!Example of test !!Example of test !!Example of test !!Example of test !!Example of test !!Example of test !!Example of test !!Example of test !!Example of test !!Example of test !!Example of test !!Example of test !!Example of test !!Example of test !!Example of test !!Example of test !!Example of test !!Example of test !!Example of test !!Example of test !!Example of test !!Example of test !!Example of test !!Example of test !!Example of test !!Example of test !!Example of test !!Example of test !!Example of test !!Example of test !!Example of test !!Example of test !!Example of test !!Example of test !!Example of test !!Example of test !!Example of test !!Example of test !!Example of test !!Example of test !!Example of test !!Example of test !!Example of test !!Example of test !!Example of test !!Example of test !!Example of test !!Example of test !!Example of test !!Example of test !!Example of test !!Example of test !!Example of test !!Example of test !!Example of test !!Example of test !!Example of test !!Example of test !!Example of test !!Example of test !!Example of test !!Example of test !!Example of test !!Example of test !!Example of test !!Example of test !!Example of test !!Example of test !!Example of test !!Example of test !!Example of test !!Example of test !!Example of test !!Example of test !!Example of test !!Example of test !!Example of test !!Example of test !!Example of test !!Example of test !!Example of test !!Example of test !!Example of test !!Example of test !!Example of test !!Example of test !!Example of test !!Example of test !!Example of test !!Example of test !!Example of test !!Example of test !!Example of test !!Example of test !!Example of test !!Example of test !!</p>" +
                          "</body>" +
                        "</html>";
		m_webview.loadData(html, "text/html", "utf-8");
	}
	

	public void initLanguagesSpiner(View v) {
		Spinner spinner = (Spinner) getView().findViewById(R.id.language_spinner);
		// Create an ArrayAdapter using the string array and a default spinner layout
		//CustomSpinnerAdapter<CharSequence> adapter = new CustomSpinnerAdapter(getActivity(),
			//	R.array.planets_array, android.R.layout.simple_spinner_item);
		 //Specify the layout to use when the list of choices appears
		
		// Apply the adapter to the spinner
		String []language_data={"Armenian","Arabian","English","Franche"};
		//in custom adapter averrided getView function which helps create custom view for spinner.
		CustomSpinnerAdapter adapter = new CustomSpinnerAdapter(getActivity().getBaseContext(), R.layout.home_page, language_data);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner.setAdapter(adapter);
	}

	private void initCountriesButton() {
		Button showCountriesFragment=(Button)getView().findViewById(R.id.CountriesButton);
		showCountriesFragment.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				FragmentManager fragmentManager = getFragmentManager();
				FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

				CountriesFragment countriesFragment = new CountriesFragment();
				fragmentTransaction.replace(R.id.realtabcontent, countriesFragment);
				//provide the fragment ID of your first fragment which you have given in
				//fragment_layout_example.xml file in place of first argument
				fragmentTransaction.addToBackStack(null);
				fragmentTransaction.commit();
			}
		});
	}
}
